package myky.com.doctorapp.presenter

import io.reactivex.disposables.Disposable
import myky.com.doctorapp.base.BasePresenterIm
import myky.com.doctorapp.base.RxPageSubscribe
import myky.com.doctorapp.base.RxSubscribe
import myky.com.doctorapp.contract.DoctorListContract
import myky.com.doctorapp.entity.DepatmentAndAreaEntity
import myky.com.doctorapp.entity.DoctorWrapperEntity
import myky.com.doctorapp.entity.PageEntity
import myky.com.doctorapp.model.DoctorListModel

class DoctorListPresenter : BasePresenterIm<DoctorListContract.View>(), DoctorListContract.Presenter {
    override fun findDeapartmentAndArea(version: String) {

        doctorListModel.findDeparmentAndArea(version).subscribe(object : RxSubscribe<DepatmentAndAreaEntity.DataEntity>() {
            override fun onFailed(code: Int, msg: String?) {
                mView.logCat(msg)
            }

            override fun onSubscribe(d: Disposable) {
                addSubscription(d)
            }

            override fun onSuccess(t: DepatmentAndAreaEntity.DataEntity) {
                val deapartment = t.department
                val area = t.area


                //插入一条全部科室

                val deapartmentCh = DepatmentAndAreaEntity.DataEntity.DepartmentEntity.DepartmentchildEntity()
                deapartmentCh.departmentchildname = "全部科室"

                val departmen = DepatmentAndAreaEntity.DataEntity.DepartmentEntity()
                departmen.departmentname = "全部科室"

                deapartment.add(0,departmen)

                var temp1 = ArrayList<DepatmentAndAreaEntity.DataEntity.DepartmentEntity.DepartmentchildEntity>()

                temp1.add(deapartmentCh)

                deapartment[0].departmentchild=temp1

                //插入一条全部地区

                val cityEntity = DepatmentAndAreaEntity.DataEntity.AreaEntity.CityEntity()
                cityEntity.cityName = "全部地区"

                val areaEntity = DepatmentAndAreaEntity.DataEntity.AreaEntity()
                areaEntity.province = "全部地区"
                area.add(0, areaEntity)

                var temp = ArrayList<DepatmentAndAreaEntity.DataEntity.AreaEntity.CityEntity>()

                temp.add(cityEntity)

                area[0].city = temp

                mView.showDeapartmentAndArea(t)
            }
        })


    }


    val doctorListModel = DoctorListModel()


    override fun findDoctorList(version: String, customerId: String, size: Int, current: Int, type: String, searchScope: Int, input: String, province: String, city: String, departmentname: String) {

        doctorListModel.findDoctorList(version, customerId, size, current, type, searchScope, input, province, city, departmentname)
                .subscribe(object : RxPageSubscribe<DoctorWrapperEntity.DoctorListEntity>() {
                    override fun onSuccess(t: DoctorWrapperEntity.DoctorListEntity?, pageEntity: PageEntity) {
                        t?.let {
                            val doctors = t.doctors
                            val isEmpy = t.isEmpty
                            if (isEmpy.equals(1)) {
                                mView.showDefaultDoctor(doctors, pageEntity)
                            } else {
                                mView.showDoctorList(doctors, pageEntity)
                            }
                            doctors
                        }
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        mView.logCat(msg)

                    }

                    override fun onSubscribe(d: Disposable) {

                        addSubscription(d)
                    }
                })
    }


}
